#!/bin/bash
pgsql_ver=12.3
pgsql_install_dir=/usr/local/pgsql
pgsql_data_dir=/opt/pgdata
dbpostgrespwd=postgres
THREAD=4

apt-get -y install zlib* libreadline-gplv2-dev
id -u postgres >/dev/null 2>&1
[ $? -ne 0 ] && useradd -d ${pgsql_install_dir} -s /bin/bash postgres
mkdir -p ${pgsql_data_dir}
chown postgres.postgres -R ${pgsql_data_dir}
wget -c https://mirrors.tuna.tsinghua.edu.cn/postgresql/latest/postgresql-${pgsql_ver}.tar.bz2
tar xjvf postgresql-${pgsql_ver}.tar.bz2
cd postgresql-${pgsql_ver}
./configure --prefix=$pgsql_install_dir
make -j ${THREAD}
make install
#
chmod 755 ${pgsql_install_dir}
chown -R postgres.postgres ${pgsql_install_dir}
if [ -e /bin/systemctl ]; then
    /bin/cp ../postgresql.service /lib/systemd/system/
    sed -i "s@=/usr/local/pgsql@=${pgsql_install_dir}@g" /lib/systemd/system/postgresql.service
    sed -i "s@PGDATA=.*@PGDATA=${pgsql_data_dir}@" /lib/systemd/system/postgresql.service
    systemctl enable postgresql
else
    /bin/cp ./contrib/start-scripts/linux /etc/init.d/postgresql
    sed -i "s@^prefix=.*@prefix=${pgsql_install_dir}@" /etc/init.d/postgresql
    sed -i "s@^PGDATA=.*@PGDATA=${pgsql_data_dir}@" /etc/init.d/postgresql
    chmod +x /etc/init.d/postgresql
    [ "${PM}" == 'yum' ] && {
        chkconfig --add postgresql
        chkconfig postgresql on
    }
    [ "${PM}" == 'apt-get' ] && update-rc.d postgresql defaults
fi

su - postgres -c "${pgsql_install_dir}/bin/initdb -D ${pgsql_data_dir}"
service postgresql start
sleep 5
su - postgres -c "${pgsql_install_dir}/bin/psql -c \"alter user postgres with password '$dbpostgrespwd';\""
sed -i 's@^host.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
sed -i 's@^local.*@#&@g' ${pgsql_data_dir}/pg_hba.conf
echo 'local   all             all                                     md5' >>${pgsql_data_dir}/pg_hba.conf
echo 'host    all             all             0.0.0.0/0               md5' >>${pgsql_data_dir}/pg_hba.conf
sed -i "s@^#listen_addresses.*@listen_addresses = '*'@" ${pgsql_data_dir}/postgresql.conf
service postgresql reload

[ -z "$(grep ^'export PATH=' /etc/profile)" ] && echo "export PATH=${pgsql_install_dir}/bin:\$PATH" >>/etc/profile
[ -n "$(grep ^'export PATH=' /etc/profile)" -a -z "$(grep ${pgsql_install_dir} /etc/profile)" ] && sed -i "s@^export PATH=\(.*\)@export PATH=${pgsql_install_dir}/bin:\1@" /etc/profile
. /etc/profile
[ "${PM}" == 'apt-get' ] && systemctl status postgresql.service